package site.xuxingcan.adbservice;

import android.accessibilityservice.AccessibilityService;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

import java.util.List;

public class MyAccessibilityService extends AccessibilityService {
    private static final String liaoTianEditTextId = "com.tencent.mm:id/z4";
    private static final String liaoTianSendId = "com.tencent.mm:id/z_";
    private static final String liaoTianBackLayoutId = "com.tencent.mm:id/ew";
    private boolean canReply = false;//能否回复且每次收到消息只回复一次

    /**
     * 处理相关事件
     */
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        int eventType = event.getEventType();
        switch (eventType) {
            //第一步：监听通知栏消息
            case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
                List<CharSequence> texts = event.getText();
                if (!texts.isEmpty()) {
                    String message = texts.get(0).toString();
                    //过滤微信内部通知消息
                    if (isInside(message)) {
                        return;
                    }
                    //模拟打开通知栏消息
                    if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) {
                        Log.i("demo", "标题栏canReply=true");
                        canReply = true;
                        try {
                            Notification notification = (Notification) event.getParcelableData();
                            PendingIntent pendingIntent = notification.contentIntent;
                            pendingIntent.send();
                        } catch (CanceledException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                }
                break;
            //第二步：监听是否进入微信聊天界面
            case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
                if (canReply) {
                    try {
                        Thread.sleep(500);
                        AccessibilityNodeInfo editText = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(liaoTianEditTextId).get(0);
                        canReply = false;
                        editText.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        Thread.sleep(500);
                        editText.performAction(AccessibilityNodeInfo.ACTION_PASTE);
                        Thread.sleep(500);
                        getRootInActiveWindow().findAccessibilityNodeInfosByViewId(liaoTianSendId).get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        Thread.sleep(500);
                        getRootInActiveWindow().findAccessibilityNodeInfosByViewId(liaoTianBackLayoutId).get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }

    //常见的微信内部通知，可自行测试并修改
    private boolean isInside(String msg) {
        boolean result = false;
        if (msg.equals("已复制") || msg.equals("已分享") || msg.equals("已下载"))
            result = true;
        if (msg.length() > 6 && (msg.substring(0, 6).equals("当前处于移动") || msg.substring(0, 6).equals("无法连接到服") || msg.substring(0, 6).equals("图片已保存至") || msg.substring(0, 6).equals("网络连接不可")))
            result = true;
        return result;
    }

    @Override
    public void onInterrupt() {
        Toast.makeText(this, "微信助手服务被中断啦~", Toast.LENGTH_LONG).show();
    }

    //服务开启时初始化
    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Toast.makeText(this, "已开启微信助手服务", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "已关闭微信助手服务", Toast.LENGTH_LONG).show();
    }
}